package org.compiere.model;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;

import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.Env;

public class Mhrmvacationline extends X_hrm_vacation_line {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3309892180545411545L;
	
	/**	Static Logger	*/
	private static CLogger	s_log	= CLogger.getCLogger (Mhrmvacationline.class);
	
	public Mhrmvacationline(Properties ctx, int hrm_vacation_line_ID,
			String trxName) {
		super(ctx, hrm_vacation_line_ID, trxName);
		// 
	}
	public Mhrmvacationline(Properties ctx, ResultSet rs, String trxName) {
		super(ctx, rs, trxName);
		// 
	}
	
	@Override
	protected boolean afterSave(boolean newRecord, boolean success) {

		if(!newRecord){
			
			Mhrmreservesectional[] line = Mhrmreservesectional.getMhrmreservesectional(); 
			for(Mhrmreservesectional l: line){
				X_hrm_vacationline vacLine = Mhrmvacationline.getVacationLineAcct(gethrm_vacation_line_ID(), l.gethrm_reservesectional_ID());
				if(vacLine == null)
					vacLine = new X_hrm_vacationline(Env.getCtx(),0,null);
				
				if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CHARGE){
					vacLine.sethrm_amount(gethrm_total());
					vacLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CHARGE);
				}
				else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_CNALOG){
					vacLine.sethrm_amount(gethrm_CNalog());
					vacLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_CNALOG);
				}
				else if(l.gethrm_reservesectional_ID() == Mhrmreserve.RESERVE_SECTIONAL_COTCH){
					vacLine.sethrm_amount(gethrm_COtch());
					vacLine.sethrm_reservesectional_ID(Mhrmreserve.RESERVE_SECTIONAL_COTCH);
				}
				vacLine.sethrm_reserve_ID(Mhrmreserve.RESERVE_VACATION);
				vacLine.sethrm_vacation_ID(gethrm_vacation_ID());
				vacLine.sethrm_vacation_line_ID(gethrm_vacation_line_ID());
				vacLine.saveEx();
			}	
		}
		
		return true;
	}

	/**
	 * Search for entries in the vacation line
	 * 
	 * @param C_BPartner_ID
	 * @param Timestamp dt
	 * @return  Model vacation line
	 */
	public static Mhrmvacationline getVacationline(int C_BPartner_ID, Timestamp sdt, Timestamp edt) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmvacationline hrmvll = new Mhrmvacationline(Env.getCtx(), 0, null);
			
		// 
		String sql_ = "SELECT * FROM HRM_VACATION_LINE \n " +
						"WHERE ISACTIVE='Y' \n " +
//							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							"AND STARTDATE =" + DB.TO_DATE(sdt, true) +
							"AND ENDDATE =" + DB.TO_DATE(edt, true);
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					hrmvll = new Mhrmvacationline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}	
		return hrmvll;
		
	}
	
	/**
	 * Search for entries in the -->>> vacationLine <<---
	 * 
	 * @param C_BPartner_ID
	 * @param Timestamp dt
	 * @return  Model vacation line
	 */
	public static X_hrm_vacationline getVacationLineAcct(int vacation_line_ID, int reservesectional_ID) {
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		X_hrm_vacationline hrmvll = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_VACATIONLINE WHERE HRM_VACATION_LINE_ID= " +vacation_line_ID +" \n" +
				" AND HRM_RESERVESECTIONAL_ID=" +reservesectional_ID;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					hrmvll = new X_hrm_vacationline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}	
		return hrmvll;
		
	}
	
	public static Mhrmvacationline getClose(int hrm_vacaton_id, Boolean process) {
		
		char type = 'N';
		
		if(hrm_vacaton_id == 0)
			return null;
		
		if (process )
			type = 'Y';
	
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmvacationline result = new Mhrmvacationline(Env.getCtx(), 0, null);
			
		// 
		String sql_ = "SELECT * FROM HRM_VACATION_LINE \n " +
						"WHERE ISACTIVE='Y' \n " +
							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND HRM_VACATION_ID=" + hrm_vacaton_id + " \n " +
							"AND PROCESSING ='" + type + "'";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new Mhrmvacationline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
	
	public static Mhrmvacationline[] getVacLineArray(int C_BPartner_ID, Timestamp dt) {
		
		if (C_BPartner_ID == 0)
			return null;
		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		Mhrmvacationline result = new Mhrmvacationline(Env.getCtx(), 0, null);
		
		ArrayList<Mhrmvacationline> list = new ArrayList<Mhrmvacationline>();
			
		// 
		String sql_ = "SELECT * FROM HRM_VACATION_LINE \n " +
						" WHERE ISACTIVE='Y' \n " +
//							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							" AND C_BPARTNER_ID=" + C_BPartner_ID + " \n " +
							" AND STARTDATE BETWEEN DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") \n " +
							" AND DATE_TRUNC('MONTH'," + DB.TO_DATE(dt, true) + ") + '1 MON'::INTERVAL - '1 DAY'::INTERVAL";
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				while(rs.next()){
					result = new Mhrmvacationline(Env.getCtx(), rs, null);
					list.add(result);
				}					
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return list.toArray(new Mhrmvacationline[list.size()]);		
	}
	
	public static X_hrm_vacationline getline(int hrm_vacation_id, int C_BPartner_ID, int C_Charge_ID){
		
		if (hrm_vacation_id==0)
			return null;

		//
	    PreparedStatement pstmt = null;
		ResultSet rs = null;
		X_hrm_vacationline result = null;
			
		// 
		String sql_ = "SELECT * FROM HRM_VACATIONLINE \n " +
						"WHERE ISACTIVE='Y' \n " +
//							"AND AD_CLIENT_ID=" + Env.getAD_Client_ID(Env.getCtx()) + " \n " +
							"AND C_CHARGE_ID=" + C_Charge_ID + " \n " +
							"AND hrm_vacation_id=" + hrm_vacation_id + " \n " +
							"AND C_BPartner_ID =" + C_BPartner_ID ;
			try
			{
				pstmt = DB.prepareStatement(sql_,null);
				rs = pstmt.executeQuery();	
				if(rs.next())
					result = new X_hrm_vacationline(Env.getCtx(), rs, null);
			}
			catch (SQLException e)
			{
				s_log.log(Level.SEVERE, "product", e);
			}
			finally
			{
				DB.close(rs, pstmt);
				rs = null; pstmt = null;
			}
			
		return result;		
	}
	
	private X_hrm_vacationline[] m_lines;
	
	public X_hrm_vacationline[] getVacationLinesAcct(boolean requery) {
		if (m_lines == null || m_lines.length == 0 || requery)
			m_lines = getVacationLinesAcct(null);
		set_TrxName(m_lines, get_TrxName());
		return m_lines;
	}

	private X_hrm_vacationline[] getVacationLinesAcct (String whereClause)
	{
		String whereClauseFinal = "hrm_vacation_line_ID=?";
		if (whereClause != null)
			whereClauseFinal += whereClause;
		List<Mhrmvacationline> list = new Query(getCtx(), I_hrm_vacationline.Table_Name, whereClauseFinal, get_TrxName())
										.setParameters(gethrm_vacation_line_ID())
										.list();
		return list.toArray(new X_hrm_vacationline[list.size()]);
	}	//	getLines

	
	public X_hrm_vacationline[] getVacationLinesAcct() {
		return getVacationLinesAcct(false);
	}
	/**
	 * @param fromLine
	 * @return
	 */
	public int copyVacationLinesFrom(Mhrmvacationline other) {
		int count = 0;
		X_hrm_vacationline[] fromLines = other.getVacationLinesAcct();
		for (int i = 0; i < fromLines.length; i++) {
			X_hrm_vacationline line = new X_hrm_vacationline(getCtx(), 0, get_TrxName());
			X_hrm_vacationline fromLine = fromLines[i];
			PO.copyValues (fromLine, line, fromLine.getAD_Client_ID(), fromLine.getAD_Org_ID());
			line.sethrm_vacation_line_ID(gethrm_vacation_line_ID());
			line.sethrm_vacation_ID(gethrm_vacation_ID());
			line.set_ValueNoCheck ("hrm_vacationline_ID", I_ZERO);	// new
			if (line.save(get_TrxName())) {
				count++;
			}
		}
		if (fromLines.length != count)
			log.log(Level.SEVERE, "Line difference - From=" + fromLines.length + " <> Saved=" + count);
		return count;
	}
}
